var createError = require('http-errors');
var express = require('express');
var logger = require('morgan');

const path = require('path');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const history = require('connect-history-api-fallback');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var proxy = require('./routes/proxy');

var connectHistoryApiFallback = require('connect-history-api-fallback')



var app = express();
app.use('/web', proxy);
app.use(connectHistoryApiFallback());
app.use(express.urlencoded({ extended: false }));
app.use(compression());
app.use(cookieParser());
app.use(logger('dev'));

app.use(express.static(path.join(__dirname, 'web-app/dist')));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('view options', {
    layout: false
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
